function debounce(fn = new Function, delay) {

    let flagTimer = null

    return function (...args) {

        /* 先摧毁前任立下的flag */
        if (flagTimer) {
            clearTimeout(flagTimer)
            flagTimer = null
            console.log("前任立下的flag业已摧毁✌️~");
        }

        /* 立一个新的flag：三秒后地球爆炸 */
        flagTimer = setTimeout(() => {
            // 地球爆炸
            fn.call(null, ...args)

            // 同时flag也归于尘土
            flagTimer = null
            
        }, delay);

    }

}

export default {

    // v-debounce:input="{ handler: onInput, delay: 3000 }"
    mounted(el = new HTMLElement, binding, vnode) {
        const { arg: eventType, value: { handler, delay } } = binding

        // 定义eventHandler
        vnode.eventHandler = debounce(handler, delay)
        vnode.eventType = eventType

        // 给el绑定事件
        el.addEventListener(
            vnode.eventType,
            vnode.eventHandler
        )
    },

    beforeUnmount(el = new HTMLElement, binding, vnode) {
        el.removeEventListener(
            vnode.eventType,
            vnode.eventHandler
        )
    }
}